import java.sql.*;
import java.util.Scanner;

public class Main {
    public static <Scanner> void main(String[] args) {

        //全查询
        private static void m1() {
            //1、 加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");


                //2、获取链接
                Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                        "root", "123456");


                //3、编写sql语句
                String sql = "select * from sys_user";

                //4、预处理sql
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                //5、设置参数

                //6、执行sql语句
                ResultSet resultSet = preparedStatement.executeQuery();
                //7、查询遍历结果或增删改判断是否正确
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String password = resultSet.getString("password");
                    System.out.println(id + " " + name + " " + password);
                }

                //8、返回视图
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        //m2条件查询
        private static void m2() {
            //输入查询信息
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入查询信息");
            String word = sc.next();

            //1、 加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");


                //2、获取链接
                Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                        "root", "123456");


                //3、编写sql语句
                String sql = "select * from sys_user wheree name like ?";

                //4、预处理sql
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                //5、设置参数
                preparedStatement.setObject(1, "%" + word + "%");
                //6、执行sql语句
                ResultSet resultSet = preparedStatement.executeQuery();
                //7、查询遍历结果或增删改判断是否正确
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String password = resultSet.getString("password");
                    System.out.println(id + " " + name + " " + password);
                }

                //8、返回视图
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }


        //m3增
        pravite static void m3() {
            //1、 加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");


                //2、获取链接
                Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                        "root", "123456");


                //3、编写sql语句
                String sql = "insert into sys_user(name,password) values (?,?)";

                //4、预处理sql
                PreparedStatement preparedStatement = conn.prepareStatement(sql);

                //5、设置参数
                preparedStatement.setObject(1, "张三");
                preparedStatement.setObject(2, "111");

                //6、执行sql语句
                int result = preparedStatement.executeUpdate();

                //7、查询遍历结果或增删改判断是否正确
                if (result > 0) {
                    System.out.println("增添完成");
                }

                //8、返回视图
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //m4删
        private static void m4(){
            //1、 加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");


                //2、获取链接
                Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                        "root", "123456");


                //3、编写sql语句
                String sql = "select * from sys_user wherre id = ?";

                //4、预处理sql
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                //5、设置参数
                preparedStatement.setObject(1,"3");

                //6、执行sql语句
                int result = preparedStatement.executeUpdate();

                //7、查询遍历结果或增删改判断是否正确
               if(result >0){
                   System.out.println("删除成功");
               }

                //8、返回视图
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //m5修改
        private static void m5(){
            //1、 加载驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");


                //2、获取链接
                Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                        "root", "123456");


                //3、编写sql语句
                String sql = "update sys_user set name = ?,password = ? where id = ?";

                //4、预处理sql
                PreparedStatement preparedStatement = conn.prepareStatement(sql);

                //5、设置参数
                preparedStatement.setObject(1,"lisi");
                preparedStatement.setObject(2,"222");
                preparedStatement.setObject(3,"3");

                //6、执行sql语句
              int result = preparedStatement.executeUpdate();

                //7、查询遍历结果或增删改判断是否正确
                if(result > 0){
                    System.out.println("更新成功");
                }

                //8、返回视图
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        


    }
}